package ivan.cheng.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class PreferencesUtils
{
	/**table name need to reconsider,cus we want this to be common*/
	public static final String PREFERENCES_NAME = "PermissionManager";
	public static final String KEY_NOTIFICATION_ON	= "notification_on";
	public static final String KEY_RESULT	= "result";
	public static final String KEY_AVAILABLE_VERSION	= "available_version";
	public static final String KEY_LENGTH	= "length";
	public static final String KEY_CRC 	= "crc";
	public static final String KEY_URL = "url";
	public static final String KEY_TIP 	= "tip";
	public static final String KEY_WAITING_INFOS 	= "waiting_infos";
	public static final String KEY_LAST_CHECK_TIME 	= "last_check_time";
	
	public static final String KEY_CPU_GOVERNOR = "cpu_governor";
	public static final String KEY_CPU_MIN_FREQ = "cpu_min_freq";
	public static final String KEY_CPU_MAX_FREQ = "cpu_max_freq";
	
	public static final String KEY_CURRENT_MODE = "current_mode";

	public static String getValue(Context context, String key)
	{
		return context.
			getSharedPreferences(PREFERENCES_NAME, Context.MODE_WORLD_READABLE).
			getString(key, null);
	}
	
	public static int getIntValue(Context context, String key)
	{
		return getIntValue(context, key, 0);
	}
	
	private static int getIntValue(Context context, String key, int defValue)
	{
		return context.
			getSharedPreferences(PREFERENCES_NAME, Context.MODE_WORLD_READABLE).
			getInt(key, defValue);
	}
	
	public static long getLongValue(Context context, String key)
	{
		return getLongValue(context, key, 0);
	}
	
	private static long getLongValue(Context context, String key, long defValue)
	{
		return context.
			getSharedPreferences(PREFERENCES_NAME, Context.MODE_WORLD_READABLE).
			getLong(key, defValue);
	}
	
	public static boolean getBoolValue(Context context, String key, boolean defValue)
	{
		return context.
			getSharedPreferences(PREFERENCES_NAME, Context.MODE_WORLD_READABLE)
			.getBoolean(key, defValue);
	}
	
	public static void setValue(Context context, String key, String value)
	{
        final SharedPreferences preferences = 
        	context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_WORLD_WRITEABLE);
        final SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
	}
	
	public static void setValue(Context context, String key, int value)
	{
        final SharedPreferences preferences = 
        	context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_WORLD_WRITEABLE);
        final SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(key, value);
        editor.commit();
	}
	
	public static void setValue(Context context, String key, long value)
	{
        final SharedPreferences preferences = 
        	context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_WORLD_WRITEABLE);
        final SharedPreferences.Editor editor = preferences.edit();
        editor.putLong(key, value);
        editor.commit();
	}
	
	public static void setValue(Context context, String key, boolean value)
	{
        final SharedPreferences preferences = 
        	context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_WORLD_WRITEABLE);
        final SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
	}

}
